package com.example.shoppingmall.cotroller;

import com.example.shoppingmall.config.MyLog;
import com.example.shoppingmall.pojo.Promotion;
import com.example.shoppingmall.service.PromotionService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;

@Controller
@RequestMapping("/promotion")
public class promotionController {

    @Resource
    PromotionService promotionService;

    /**
     * 查询，模糊查询，分页
     * @param dangIng
     * @param name
     * @param statusa
     * @param startTime
     * @param endTime
     * @param model
     * @return
     */
    @RequestMapping("/seckillList")
    public String seckill(
            @RequestParam(value = "dang", required = false) String dangIng,
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "status", required = false) String statusa,
            @RequestParam(value = "startTime", required = false) String startTime,
            @RequestParam(value = "endTime", required = false) String endTime,
            Model model) {
        int dang = 1;

        if (dangIng != null & dangIng != "") {
            dang = Integer.parseInt(dangIng);
        }

        if (statusa == null | statusa == "") {
            statusa = "0";
        }

        if (name==null){
            name="";
        } if (startTime==null){
            startTime="";
        } if (endTime==null){
            endTime="";
        }

        int size = 6;

        int count=promotionService.count(name,Integer.parseInt(statusa),startTime,endTime);
        int zong = count % size == 0 ? count / size : count / size + 1;
        model.addAttribute("seckill",promotionService.getAll(name,Integer.parseInt(statusa),startTime,endTime,dang,size));
        model.addAttribute("dang", dang);
        model.addAttribute("zong", zong);

        model.addAttribute("name",name);
        model.addAttribute("status",statusa);
        model.addAttribute("startTime",startTime);
        model.addAttribute("endTime",endTime);

        return "/promotion/seckill";
    }

    /**
     * 删除秒杀
     * @param id
     * @return
     */

    @MyLog(value = "促销管理->团购秒杀->删除秒杀")
    @RequestMapping("/del")
    public String del(@RequestParam("id") String id) {
        System.out.println(id);
        int num=promotionService.del(Integer.valueOf(id));
        if (num>0){
            return "redirect:/promotion/seckillList";
        }
        return "/promotion/seckill";
    }

    /**
     * 跳转到新增秒杀列表，并且查询商品
     * @param model
     * @return
     */
    @MyLog(value = "促销管理->团购秒杀->跳转新增秒杀")
    @RequestMapping("/toAddSeckill")
    public String toAddSeckill(Model model){

        model.addAttribute("getAllComm",promotionService.getAllComm());

        return "promotion/addSeckill";
    }


    /**
     * 新增秒杀
     * @param promotion
     * @return
     */
    @MyLog(value = "促销管理->团购秒杀->新增秒杀")
    @RequestMapping("/addSeckillP")
    public String addSeckill(Promotion promotion){
        int num=promotionService.add(promotion);
        if (num>0) {
            return "redirect:/promotion/seckillList";
        }
        return "promotion/addSeckill";
    }

    /**
     * 跳转到修改秒杀列表，并且查询商品，和根据id查询该秒杀信息
     * @param id
     * @param model
     * @return
     */
    @MyLog(value = "促销管理->团购秒杀->跳转修改秒杀")
    @RequestMapping("/toUpdateSeckill")
    public String toUpdateSeckill(String id,Model model){

        model.addAttribute("getAllComm",promotionService.getAllComm());
        model.addAttribute("toUpdateSeckill",promotionService.getById(Integer.valueOf(id)));

        return "/promotion/updateSeckill";
    }

    /**
     * 修改秒杀
     * @param promotion
     * @return
     */
    @MyLog(value = "促销管理->团购秒杀->修改秒杀")
    @RequestMapping("/updateSeckillP")
    public String updateSeckill(Promotion promotion){

        int num=promotionService.update(promotion);
        //System.out.println(promotion.getId());
        if (num>0) {
           return "redirect:/promotion/seckillList";
        }
      return "promotion/updateSeckill";



    }

}
